package com.bdd.controller;

import com.bdd.Result.Result;
import com.bdd.model.bo.OrderBO;
import com.bdd.model.bo.PageBO;
import com.bdd.service.OrderItemService;
import com.bdd.service.OrderService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author huanghaining
 * @date 2022/3/18
 */
@RestController
@RequestMapping("/order")
@Api(value = "订单项管理")
public class OderController {

    @Autowired
    OrderService orderService;

    @GetMapping("/orderList")
    @ResponseBody
    @ApiOperation("订单列表")
//    @RequiresRoles(value = {"STAFF", "ADMIN"}, logical = Logical.OR)
    public Result<PageInfo<OrderBO>> list(PageBO pageBO){
        if(pageBO.getPageNum()<=0){
            pageBO.setPageNum(1);
        }
        if(pageBO.getPageSize()<=0){
            pageBO.setPageSize(10);
        }
        PageInfo<OrderBO> orderBOList = new PageInfo<>(orderService.list(pageBO.getPageNum(),pageBO.getPageSize()));
        return Result.ok(orderBOList);
    }

    @PostMapping("/delete")
    @ResponseBody
    @ApiOperation("删除订单")
    public Result delete(Integer id) {
        if (Objects.isNull(id)) {
            throw new IllegalArgumentException("类别id不可为空");
        }
        orderService.delete(id);
        return Result.ok();
    }

    @GetMapping("/searchByID")
    @ResponseBody
    @ApiOperation("根据id查找订单")
    @RequiresRoles(value = {"STAFF", "ADMIN"}, logical = Logical.OR)
    public Result<List<OrderBO>> searchByID(Integer id) {
//        OrderBO orderBO = orderService.searchByOrderId(id);
//        return Result.ok(orderBO);

        List<OrderBO> orderBOList = orderService.searchByOrderId(id);
        return Result.ok(orderBOList);
    }


}
